﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DemoObject;
using BusinessObjects;

public partial class VideoCategoryList : System.Web.UI.UserControl
{
    private const string ASCENDING = " ASC";
    private const string DESCENDING = " DESC";
    private const int PageSize = 20;

    private string GridViewSortDirection
    {

        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = "ASC";
            return ViewState["sortDirection"].ToString();
        }
        set { ViewState["sortDirection"] = value; }

    }
    public int PageIndex
    {
        get
        {
            if (Request["page"] != null)
                return int.Parse(Request["page"].ToString());
            return 1;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData("");
        }

    }

    private void BindData(string strSort)
    {
        HashSet<string> hs = new HashSet<string>();
        hs.Add(strSort);
        int RowCount = 0;
        grv.DataSource = new TblVideoCategoriesRepository().SelectListItem(null, "", "", null, PageIndex, PageSize, out RowCount, hs);
        grv.DataBind();
        smartPager.PageSize = PageSize;
        smartPager.QueryStringKey = "page";
        smartPager.RowCount = RowCount;
        smartPager.UrlFormatString = "/Admin/Module/VideoCategory/VideoCategoryList.aspx?page={0}";
        smartPager.Visible = RowCount > PageSize;

    }
    protected void grv_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (GridViewSortDirection.Equals("ASC"))
            GridViewSortDirection = "DESC";
        else
            GridViewSortDirection = "ASC";
        string strSort = e.SortExpression + " " + GridViewSortDirection;
        BindData(strSort);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Admin/Module/VideoCategory/VideoCategoryList.aspx");
    }
    protected void grv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("DeleteItem"))
        {
            HiddenField hdfId = (HiddenField)grv.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("hdfId");
            TblVideoCategoriesRepository.CreateInstant().Delete(new Guid(hdfId.Value));
            Response.Redirect(Request.RawUrl);
        }
    }
}